<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="功能名称" prop="functionName">
        <el-input
          v-model="queryParams.functionName"
          placeholder="请输入功能名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="功能类型" prop="functionType">
        <el-select
          v-model="queryParams.functionType"
          placeholder="请选择功能类型"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in functionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="应用地址" prop="functionUrl">
        <el-input
          v-model="queryParams.functionUrl"
          placeholder="请输入应用地址"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:intserv:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:intserv:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:intserv:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:intserv:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="intservList"
      @selection-change="handleSelectionChange"
      :default-sort="{ prop: 'fieldSort', order: 'descending' }" @sort-change="tableSortChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="功能名称" align="center" prop="functionName" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="功能类型" align="center" prop="functionType" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <span>{{ statusFormat(scope.row.functionType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应用地址" align="center" prop="functionUrl" />
      <el-table-column label="状态" align="center" prop="status" width="100" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
       <el-table-column label="图片地址" align="center" prop="functionType">
        <template slot-scope="scope">
         <img :src="scope.row.filePath" alt=""  width="30" height="30" >
        </template>
      </el-table-column>
      <!-- <el-table-column label="图片地址" align="center" prop="filePath" /> -->
      <el-table-column label="排序" align="center" prop="fieldSort" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="备注" align="center" prop="remark" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:intserv:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:intserv:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改综合服务配合对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="功能名称" prop="functionName">
          <el-input v-model="form.functionName" placeholder="请输入功能名称" />
        </el-form-item>
        <el-form-item label="功能类型" prop="functionType">
          <el-select v-model="form.functionType" placeholder="请选择功能类型">
            <el-option
              v-for="dict in functionOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="应用地址"
          prop="functionUrl"
          v-if="form.functionType == 2"
        >
          <el-input v-model="form.functionUrl" placeholder="请输入应用地址" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="图片上传" prop="filePath">
          <el-upload
            ref="upload"
            :limit="1"
            :on-exceed="handleExceed"
            accept=".jpg, .png"
            :action="upload.url"
            :headers="upload.headers"
            :file-list="upload.fileList"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="true"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :multiple="false"
            :before-remove="beforeRemove"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button>
            <!-- <el-button
              style="margin-left: 10px"
              size="small"
              type="success"
              :loading="upload.isUploading"
              @click="submitUpload"
              >上传到服务器</el-button> -->
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="图片地址" prop="filePath">
        <div v-if="form.filePath!=null&&form.filePath!=''">
          <el-link :underline="false" icon="el-icon-paperclip" style="text-align:left; float:left; overflow:hidden;white-space:nowrap;text-overflow:ellipsis; ">{{substr(form.filePath)}}</el-link>
          <el-link @click="downloadFiles()"> <i class="el-icon-download el-icon--right" ></i>下载</el-link>
          <el-link @click="deleteFile()"> <i class="el-icon-delete el-icon--right" ></i>删除</el-link>
        </div>
        </el-form-item>
        <el-form-item label="排序" prop="fieldSort">
          <el-input-number
            :precision="2"
            :step="0.1"
            v-model="form.fieldSort"
            placeholder="请输入排序"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIntserv,
  getIntserv,
  delIntserv,
  addIntserv,
  updateIntserv,
  exportIntserv,
  changeStatus,
} from "@/api/system/intserv";
import { getToken } from "@/utils/auth";
import { download, deleteFile} from "@/api/tool/tools";
export default {
  name: "Intserv",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 综合服务配合表格数据
      intservList: [],
      //应用类型
      functionOptions: [],
      //状态类型
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        functionName: null,
        functionType: null,
        functionUrl: null,
        status: null,
        filePath: null,
        fieldSort: null,
      },
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: [],
        file: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_function_type").then((response) => {
      this.functionOptions = response.data;
    });
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询综合服务配合列表 */
    getList() {
      this.loading = true;
      listIntserv(this.queryParams).then((response) => {
        this.intservList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        functionName: null,
        functionType: null,
        functionUrl: null,
        status: "0",
        filePath: null,
        fieldSort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加综合服务配合";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getIntserv(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改综合服务配合";
      });
    },
    // 系统状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm(
        '确认要"' + text + '""' + row.functionName + '"吗?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return changeStatus(row.id, row.status);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateIntserv(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIntserv(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除综合服务配合编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delIntserv(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有综合服务配合数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportIntserv(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    //状态名称初始化
    statusFormat(functionType) {
      let functions = this.functionOptions;
      for (let i = 0; i < functions.length; i++) {
        if (functions[i].dictValue == functionType) {
          return functions[i].dictLabel;
        }
      }
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.filePath = response.url;
      this.msgSuccess(response.msg);
      fileList = [];
    },
    // 重置表单操作
    resetForm(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields();
      }
      if (this.$refs["upload"] !== undefined) {
        this.$refs.upload.clearFiles();
      }
    },
    // 文件列表移除文件时的钩子
    handleRemove(file, fileList) {
        console.log(file, fileList);
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
        console.log(file);
    },
    // 选择时事件提醒
    handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件,请删除后重新下载`);
    },
    // 移出前事件提示
    beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
    substr(str){
      if(str){
      //  return  str.substr(str.lastIndexOf("/")+1);
      return  "图片"+str.substr(str.lastIndexOf("."));
      }
    },
    downloadFiles(){
     download(this.substr(this.form.filePath),this.form.filePath);
    },
     deleteFile(){
     this.form.filePath="";
    },
  },
};
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
